﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Efcore.MultiTenant.DbProvider
{
    /// <summary>
    /// 数据库连接对象，若没有对应数据库key,则默认使用第一个数据库
    /// </summary>
    public class ConnectionResolve<TDbContext> : IConnectionResolve<TDbContext> where TDbContext : BaseDbContext
    {
        public DbType DbType { get; }

        public string ConnectionString { get; }

        public TenantType TenantType { get; }

        public TenantInfo TenantInfo { get; set; }

        public ConnectionResolve(TenantInfo tenantInfo, TenantOption<TDbContext> tenantOption)
        {
            TenantInfo = tenantInfo;
            TenantType = tenantOption.TenantType;
            var dbOption = tenantOption.DbOptionList.FirstOrDefault(m => m.DbKey == tenantInfo.GetDbKey());
            dbOption ??= tenantOption.DbOptionList[0];
            ConnectionString = dbOption.DbConnectionString;
            DbType = tenantOption.DbType;
        }
    }
}
